﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMN.Models;
using CMN.Entity;
using CMN.Common;
using CMN.Config;
using CMN.Models.Interfaces;
using System.Text.RegularExpressions;
namespace CMN.Forms
{
    public partial class frmAddUrlLog : Form
    {
        public int Id = 0;
        public frmAddUrlLog()
        {
            InitializeComponent();
            string strPath = Utils.GetServerPath() + "siteTemp.xml";
            XmlHelper xml = new XmlHelper(strPath, true);
            tbSendLoginName.Text = xml.GetText("root/SendUrlLog/SendLoginName");
           tbSeoSiteUrl.Text= xml.GetText("root/SendUrlLog/SEOSiteUrl");
            xml.Dispose();
        }
        public frmAddUrlLog(int id)
        {
            InitializeComponent();
            Id = id;
            IDataService<SendUrlLogEntity> urlLogSv = ServiceBuilder.BuildSendUrlLogService();
            SendUrlLogEntity urlLog = urlLogSv.Look(Id);
            if (urlLog != null)
            {
                tbIsIndex.Text = urlLog.IsIndex;
                tbSendLoginName.Text = urlLog.SendLoginName;
                tbSendUrl.Text = urlLog.SendUrl;
              tbSEOKeyWord.Text = urlLog.SEOKeyWord;
                tbSeoSiteUrl.Text = urlLog.SEOSiteUrl;
                tbSEOUrl.Text = urlLog.SEOUrl;
                tbSiteId.Text = urlLog.SiteId.ToString();
               
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbSendUrl.Text))
            {
                MessageBox.Show("请填写网站URL");
                return;
            }
            btnAdd.Enabled = false;
            SendUrlLogEntity url = new SendUrlLogEntity();
            Uri baseUri;
            string siteUrl = "";
            // site.BR = Utils.StringToInt(tbSeoSiteUrl.Text,0);
            url.IsIndex = tbIsIndex.Text;
            url.SendLoginName = tbSendLoginName.Text;
            url.SEOUrl = tbSEOUrl.Text;
            url.SEOSiteUrl = tbSeoSiteUrl.Text;
            if (!string.IsNullOrEmpty(url.SEOUrl))
            {
                baseUri = new Uri(url.SEOUrl);
                siteUrl = "http://" + baseUri.Authority.ToLower();

                url.SEOSiteUrl = siteUrl;
            }
            if (!string.IsNullOrEmpty(url.SEOSiteUrl))
            {
                baseUri = new Uri(url.SEOSiteUrl);
                siteUrl = "http://" + baseUri.Authority.ToLower();

                url.SEOSiteUrl = siteUrl;
            }
            if (tbSendLoginName.Text != "")
            {
                string strPath = Utils.GetServerPath() + "siteTemp.xml";
                XmlHelper xml = new XmlHelper(strPath,true);
                string strLoginName = xml.GetText("root/SendUrlLog/SendLoginName");
                string strSEOSiteUrl = xml.GetText("root/SendUrlLog/SEOSiteUrl");
                if (strLoginName != tbSendLoginName.Text)
                {
                    if (string.IsNullOrEmpty(strLoginName))
                    {
                        xml.InsertNode("root", "SendUrlLog", "SendLoginName", tbSendLoginName.Text);
                    }
                    else
                        xml.Update("root/SendUrlLog/SendLoginName", tbSendLoginName.Text);
                    
                    
                }
                if (!string.IsNullOrEmpty(url.SEOSiteUrl))
                {
                    baseUri = new Uri(url.SEOSiteUrl);
                    siteUrl = "http://" + baseUri.Authority.ToLower();

                    url.SEOSiteUrl = siteUrl;
                    if (string.IsNullOrEmpty(strSEOSiteUrl))
                    {
                        xml.InsertNode("root", "SendUrlLog", "SEOSiteUrl", url.SEOSiteUrl);
                    }
                    else
                        xml.Update("root/SendUrlLog/SEOSiteUrl", url.SEOSiteUrl);
                }
                xml.Save();
                xml.Dispose();
            }
            url.SendUrl = tbSendUrl.Text;
            url.AddDate = DateTime.Now;
            
            url.SEOKeyWord = tbSEOKeyWord.Text;
          

           
           
            try
            {

                baseUri = new Uri(tbSendUrl.Text);
                siteUrl = "http://" + baseUri.Authority.ToLower();
                url.SendUrl = tbSendUrl.Text;
                int recordNum = 0;
                QueryParam qp = new QueryParam();
                IDataService<SendUrlLogEntity> SendUrlLogSv = ServiceBuilder.BuildSendUrlLogService();
                
                if (Id == 0)
                {
                    qp.Where = string.Concat(" SendUrl='", url.SendUrl, "'");
                    qp.PageSize = 1;

                    List<SendUrlLogEntity> urlLogList = SendUrlLogSv.List(qp, out recordNum);
                    if (urlLogList.Count > 0)
                    {
                        MessageBox.Show("此外链已添加过");
                        tbSendUrl.Text = "";
                        btnAdd.Enabled = true;
                        return;
                    }
                    IDataService<SiteInfoEntity> SiteInfoSv = ServiceBuilder.BuildSiteInfoService();
                    qp = new QueryParam();
                    baseUri = new Uri(tbSendUrl.Text);
                    siteUrl = "http://" + baseUri.Authority.ToLower();
                    qp.PageSize = 1;
                    qp.Where = string.Concat(" siteUrl='", siteUrl, "'");
                    List<SiteInfoEntity> siteList = SiteInfoSv.List(qp, out  recordNum);
                    if (siteList.Count > 0)
                    {
                        url.SiteId = siteList[0].Id;
                    }
                    else
                    {
                        frmAddSite frmSite = new frmAddSite();
                        SiteInfoEntity site = frmSite.GetSiteInfo(siteUrl);
                        url.SiteId = Utils.StringToInt(SiteInfoSv.Insert(site), 0);
                    }
                    SendUrlLogSv.Insert(url);
                }
                else
                {
                    url.Id = Id;
                    url.SiteId = Utils.StringToInt(tbSiteId.Text, 0);
                    SendUrlLogSv.Update(url);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            clear();
            btnAdd.Enabled = true;
            if (tbSendLoginName.Text != "")
            {
                XmlHelper xmlHelper = new XmlHelper(string.Concat(Utils.GetServerPath(), "temp.xml"), true);
                xmlHelper.InsertNode("root", "UrlLog", "LoginName", tbSendLoginName.Text);
                xmlHelper.Dispose();
            }

            //List<SiteInfoEntity> sys_moduleList = SiteInfoSv.List(qp, out recordNum);
        }
        private void clear()
        {
            //tbSeoSiteUrl.Text = "";
            //tbIsIndex.Text = "";
            tbSEOUrl.Text = "";
            tbSiteId.Text = "";
            tbSendUrl.Text = "";
            //tbSendLoginName.Text = "";
        }

      
       
    }
}
